snippet kahn "kahn,求拓扑排序" b

/* ================= 拓扑排序 =================*/
int kahn(){
    //count用于计算输出的顶点个数
    int count=0;
    int i,j,k;
    //把入度为0的顶点入栈
    for (i=1;i<=n;i++){
        if( indgree[i] == 0)
            push(i);
    }

    while (!empty()) {//如果栈为空，则结束循环
        int t = pop();
        printf("%d ",t); //输出
        count++;

        // t点周围的点,入度-1
        for(i=first[t];i!=-1;i = e[i].next){
            int v = e[i].v;
            indgree[v]--;
            if( indgree[v] == 0) //如果入度减少到为0，则入栈
                push(v);
        }
    }

    return count;
}
/* ================= 拓扑排序 end =================*/
${0}
endsnippet

